package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.ProjectSearchAdapter;
import com.hongxun.design.bean.SearchBean;
import com.hongxun.design.bean.UnitBean;
import com.hongxun.design.fragments.ProjectFragment;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.FragmentUtil;

import java.util.List;


/**
 * Created by zerdoor_pc on 2016/6/12.
 * 项目搜索
 */
public class ProjectSearchActivity extends BaseActivity {
    TextView search_back, search_icon;
    LinearLayout search_title;
    EditText search_input;
    ListView search_list;
    List<SearchBean> data;
    private Handler mHandler;
    public final int UPDATE = 1;
    public final int SEARCH = 2;
    public static ProjectSearchAdapter adapter;
    ProjectFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_projectsearch);
        findView();
        initialization();

    }


    @Override
    protected void onResume() {
        MyApp.activity = this;
        mActivity = this;
        baseActivity = this;
        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();
        search_back = (TextView) findViewById(R.id.search_back);
        search_title = (LinearLayout) findViewById(R.id.search_title2);
        search_icon = (TextView) findViewById(R.id.search_icon2);
        search_input = (EditText) findViewById(R.id.search_input2);
        SetTypeface.set(search_icon, IconSourse.SEARCH);//搜索
        SetTypeface.set(search_back, IconSourse.BACK);
        search_list = (ListView) findViewById(R.id.search_list);
    }

    @Override
    protected void initialization() {
        super.initialization();
        addHandler();
        fragment = FragmentUtil.getIntence(mActivity).projectFragment;
        search_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        data = DataUtil.getSearchBeanList(fragment);
        adapter = new ProjectSearchAdapter(this, data, mHandler);
        search_list.setAdapter(adapter);
        search_input.setText(fragment.project_name);
    }

    private void addHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (fragment == null) fragment = FragmentUtil.getIntence(mActivity).projectFragment;
                switch (msg.what) {
                    case UPDATE:
                        fragment.project_name = "";
                        fragment.start_year = "";
                        fragment.end_year = "";
                        fragment.current_stage = "";
                        fragment.project_type = "";
                        fragment.current_schedule = "";
                        fragment.owner_unit = "";
                        fragment.design_unit = "";
                        fragment.owner_unit_string = "";
                        fragment.design_unit_string = "";
                        search_input.setText("");
                        if (data == null) {
                            data = DataUtil.getSearchBeanList(fragment);
                        } else {
                            data.clear();
                            data.addAll(DataUtil.getSearchBeanList(fragment));
                        }
                        adapter.notifyDataSetChanged();
                        fragment.onRefresh();
                        break;
                    case SEARCH:
                        fragment.project_name = search_input.getText().toString();
                        for (SearchBean s : data) {
                            if (s.getValueCode().toString().length() > 0) {
                                switch (s.getNameCode()) {
                                    case MString.year:
                                        String[] tar = s.getValueCode().toString().split("—");
                                        try {
                                            fragment.start_year = tar[0];
                                            fragment.end_year = tar[1];
                                        } catch (Exception e) {
                                        }
                                        break;
                                    case MString.current_stage:
                                        fragment.current_stage = (String) s.getValueCode();
                                        break;
                                    case MString.project_type:
                                        fragment.project_type = (String) s.getValueCode();
                                        break;
                                    case MString.current_schedule:
                                        fragment.current_schedule = (String) s.getValueCode();
                                        break;
                                    case MString.owner_unit:
                                        if (s.getValueCode() instanceof UnitBean) {
                                            UnitBean bean = (UnitBean) s.getValueCode();
                                            fragment.owner_unit = bean.getId();
                                            fragment.owner_unit_string = JSON.toJSONString(bean);
                                        } else {
                                            fragment.owner_unit = "";
                                            fragment.owner_unit_string = "";
                                        }

                                        break;
                                    case MString.design_unit:
                                        if (s.getValueCode() instanceof UnitBean) {
                                            UnitBean bean2 = (UnitBean) s.getValueCode();
                                            MyLog.L(MString.SUCCESSED + " UnitBean=" + JSON.toJSONString(bean2));
                                            fragment.design_unit = bean2.getId();
                                            fragment.design_unit_string = JSON.toJSONString(bean2);
                                        } else {
                                            fragment.design_unit = "";
                                            fragment.design_unit_string = "";
                                        }
                                        break;
                                }

                            }

                        }

                        fragment.onRefresh();
                        mActivity.finish();
                        break;

                }
            }
        };
    }


    private void search() {
        //MyApp.HTTP.getSearchProject(mHandler,HttpStringUtil.SUCCESSED,);
    }
}
